/*=========================================================================
Elite College as Engines of Upward Mobility: Evidence from Colombia's Ser Pilo Paga
Authors: Juliana Londoño-Vélez, Catherine Rodriguez, Fabio Sánchez
and Luis Esteban Álvarez-Arango

Creation date: June 6th, 2025
--------------------------------------------------------------------------
Figures 9a, 9b, 9c, 9d, 9e, 10a, 10b, 11a and 11b
=========================================================================*/

use "${data}/data_RD", clear
keep if icfes_per==20142 | icfes_per==20132 | icfes_per==20122 
drop if icfes_stratum=="Sin Estrato"
drop if puntaje_global_s11_final==.

gegen aux=tag(codigo_ies icfes_per) if access_0==1
gegen nump=total(aux), by(codigo_ies)
tab nump,m
keep if nump==3 | nump==0
drop if codigo_ies!=. & nump==0
gen d_post=(icfes_per==20142)
replace codigo_programa=. if access_6==0
replace codigo_programa=0 if codigo_programa==.
rename codigo_programa codigo_prog

xtile decile122=puntaje_global_s11_final if icfes_per==20122, nq(10)
xtile decile132=puntaje_global_s11_final if icfes_per==20132, nq(10)
xtile decile142=puntaje_global_s11_final if icfes_per==20142, nq(10)
gen decile=decile142
replace decile=decile132 if icfes_per==20132
replace decile=decile122 if icfes_per==20122
drop decile1*

replace decile=5 if decile<5
gen d0=.
gen d1=.
forvalues i=5/10{
	replace d0=`i' + 0.1 in `=`i'-4'
	replace d1=`i' - 0.1 in `=`i'-4'
	gen post_d`i'=(d_post==1 & decile==`i')
}

label define d0 5 "5 and below"
label values d0 d0

forvalues j=0/1{
	reghdfe access_0_hq post_d6 post_d7 post_d8 post_d9 post_d10  if eligible_sisben==`j', abs(i.icfes_per i.decile) vce(robust)
	cap drop b`j' ub`j' lb`j'
	matrix list e(b)
	foreach i in b se{
		mat `i'`j' = [0\_`i'[post_d6]\_`i'[post_d7]\_`i'[post_d8]\_`i'[post_d9]\_`i'[post_d10]]
	}
	mat ub`j' = b`j'+((1.96)*se`j')
	mat lb`j' = b`j'-((1.96)*se`j')
	mat data`j' = [b`j', ub`j', lb`j']
	svmat data`j'
	rename data`j'1 b`j'
	rename data`j'2 ub`j'
	rename data`j'3 lb`j'
}

recode ub0 ub1 lb0 lb1 (0=.)

#delimit ;
twoway 	(scatter b0 d0 if !missing(b0), color(blue) m(Th) lwidth(thick) msize(medlarge))
		(rcap ub0 lb0 d0 if !missing(b0), color(blue) m(o) msiz(vlarge) lwidth(medthick))
		(scatter b1 d1 if !missing(b1), color(red) m(o) lwidth(thick) msize(medlarge))
		(rcap ub1 lb1 d1 if !missing(b1), color(red) m(o) msiz(vlarge) lwidth(medthick))
		, ytitle(DD Coefficient and 95% CI) xtitle(SABER 11 Decile) 
		xlab(5(1)10, valuelabel) ysc(r(-.2 .45)) ylab(-.15(.15).45) xsize(8) 
		legend(order(3 "Low SES" 1 "High SES")
		position(10) ring(0) size(medlarge))
		yline(0, lc(gray) lpattern(dash) lwidth(thin))
;
#delimit cr
graph export "${outcomes}/Fig9A.pdf", replace

forvalues j=0/1{
	reghdfe access_0_hq_pri post_d6 post_d7 post_d8 post_d9 post_d10  if eligible_sisben==`j', abs(i.icfes_per i.decile) vce(robust)
	cap drop b`j' ub`j' lb`j'
	matrix list e(b)
	foreach i in b se{
		mat `i'`j' = [0\_`i'[post_d6]\_`i'[post_d7]\_`i'[post_d8]\_`i'[post_d9]\_`i'[post_d10]]
	}
	mat ub`j' = b`j'+((1.96)*se`j')
	mat lb`j' = b`j'-((1.96)*se`j')
	mat data`j' = [b`j', ub`j', lb`j']
	svmat data`j'
	rename data`j'1 b`j'
	rename data`j'2 ub`j'
	rename data`j'3 lb`j'
}

recode ub0 ub1 lb0 lb1 (0=.)

#delimit ;
twoway 	(scatter b0 d0 if !missing(b0), color(blue) m(Th) lwidth(thick) msize(medlarge))
		(rcap ub0 lb0 d0 if !missing(b0), color(blue) m(o) msiz(vlarge) lwidth(medthick))
		(scatter b1 d1 if !missing(b1), color(red) m(o) lwidth(thick) msize(medlarge))
		(rcap ub1 lb1 d1 if !missing(b1), color(red) m(o) msiz(vlarge) lwidth(medthick))
		, ytitle(DD Coefficient and 95% CI) xtitle(SABER 11 Decile) 
		xlab(5(1)10, valuelabel) ysc(r(-.2 .45)) ylab(-.15(.15).45) xsize(8) 
		legend(order(3 "Low SES" 1 "High SES")
		position(10) ring(0) size(medlarge))
		yline(0, lc(gray) lpattern(dash) lwidth(thin))
;
#delimit cr
graph export "${outcomes}/Fig9B.pdf", replace

forvalues j=0/1{
	reghdfe access_0_hq_pub post_d6 post_d7 post_d8 post_d9 post_d10  if eligible_sisben==`j', abs(i.icfes_per i.decile) vce(robust)
	cap drop b`j' ub`j' lb`j'
	matrix list e(b)
	foreach i in b se{
		mat `i'`j' = [0\_`i'[post_d6]\_`i'[post_d7]\_`i'[post_d8]\_`i'[post_d9]\_`i'[post_d10]]
	}
	mat ub`j' = b`j'+((1.96)*se`j')
	mat lb`j' = b`j'-((1.96)*se`j')
	mat data`j' = [b`j', ub`j', lb`j']
	svmat data`j'
	rename data`j'1 b`j'
	rename data`j'2 ub`j'
	rename data`j'3 lb`j'
}

recode ub0 ub1 lb0 lb1 (0=.)

#delimit ;
twoway 	(scatter b0 d0 if !missing(b0), color(blue) m(Th) lwidth(thick) msize(medlarge))
		(rcap ub0 lb0 d0 if !missing(b0), color(blue) m(o) msiz(vlarge) lwidth(medthick))
		(scatter b1 d1 if !missing(b1), color(red) m(o) lwidth(thick) msize(medlarge))
		(rcap ub1 lb1 d1 if !missing(b1), color(red) m(o) msiz(vlarge) lwidth(medthick))
		, ytitle(DD Coefficient and 95% CI) xtitle(SABER 11 Decile) 
		xlab(5(1)10, valuelabel) ysc(r(-.2 .45)) ylab(-.15(.15).45) xsize(8) 
		legend(order(3 "Low SES" 1 "High SES")
		position(10) ring(0) size(medlarge))
		yline(0, lc(gray) lpattern(dash) lwidth(thin))
;
#delimit cr
graph export "${outcomes}/Fig9C.pdf", replace

forvalues j=0/1{
	reghdfe access_0_lq post_d6 post_d7 post_d8 post_d9 post_d10  if eligible_sisben==`j', abs(i.icfes_per i.decile) vce(robust)
	cap drop b`j' ub`j' lb`j'
	matrix list e(b)
	foreach i in b se{
		mat `i'`j' = [0\_`i'[post_d6]\_`i'[post_d7]\_`i'[post_d8]\_`i'[post_d9]\_`i'[post_d10]]
	}
	mat ub`j' = b`j'+((1.96)*se`j')
	mat lb`j' = b`j'-((1.96)*se`j')
	mat data`j' = [b`j', ub`j', lb`j']
	svmat data`j'
	rename data`j'1 b`j'
	rename data`j'2 ub`j'
	rename data`j'3 lb`j'
}

recode ub0 ub1 lb0 lb1 (0=.)

#delimit ;
twoway 	(scatter b0 d0 if !missing(b0), color(blue) m(Th) lwidth(thick) msize(medlarge))
		(rcap ub0 lb0 d0 if !missing(b0), color(blue) m(o) msiz(vlarge) lwidth(medthick))
		(scatter b1 d1 if !missing(b1), color(red) m(o) lwidth(thick) msize(medlarge))
		(rcap ub1 lb1 d1 if !missing(b1), color(red) m(o) msiz(vlarge) lwidth(medthick))
		, ytitle(DD Coefficient and 95% CI) xtitle(SABER 11 Decile) 
		xlab(5(1)10, valuelabel) ysc(r(-.2 .45)) ylab(-.15(.15).45) xsize(8) 
		legend(order(3 "Low SES" 1 "High SES")
		position(10) ring(0) size(medlarge))
		yline(0, lc(gray) lpattern(dash) lwidth(thin))
;
#delimit cr
graph export "${outcomes}/Fig9D.pdf", replace

forvalues j=0/1{
	reghdfe access_6 post_d6 post_d7 post_d8 post_d9 post_d10  if eligible_sisben==`j', abs(i.icfes_per i.decile) vce(robust)
	cap drop b`j' ub`j' lb`j'
	matrix list e(b)
	foreach i in b se{
		mat `i'`j' = [0\_`i'[post_d6]\_`i'[post_d7]\_`i'[post_d8]\_`i'[post_d9]\_`i'[post_d10]]
	}
	mat ub`j' = b`j'+((1.96)*se`j')
	mat lb`j' = b`j'-((1.96)*se`j')
	mat data`j' = [b`j', ub`j', lb`j']
	svmat data`j'
	rename data`j'1 b`j'
	rename data`j'2 ub`j'
	rename data`j'3 lb`j'
}

recode ub0 ub1 lb0 lb1 (0=.)

#delimit ;
twoway 	(scatter b0 d0 if !missing(b0), color(blue) m(Th) lwidth(thick) msize(medlarge))
		(rcap ub0 lb0 d0 if !missing(b0), color(blue) m(o) msiz(vlarge) lwidth(medthick))
		(scatter b1 d1 if !missing(b1), color(red) m(o) lwidth(thick) msize(medlarge))
		(rcap ub1 lb1 d1 if !missing(b1), color(red) m(o) msiz(vlarge) lwidth(medthick))
		, ytitle(DD Coefficient and 95% CI) xtitle(SABER 11 Decile) 
		xlab(5(1)10, valuelabel) ysc(r(-.2 .45)) ylab(-.15(.15).45) xsize(8) 
		legend(order(3 "Low SES" 1 "High SES")
		position(10) ring(0) size(medlarge))
		yline(0, lc(gray) lpattern(dash) lwidth(thin))
;
#delimit cr
graph export "${outcomes}/Fig9E.pdf", replace

forvalues j=0/1{
	reghdfe graduation_exam_pro_7_hq post_d6 post_d7 post_d8 post_d9 post_d10  if eligible_sisben==`j', abs(i.icfes_per i.decile) vce(robust)
	cap drop b`j' ub`j' lb`j'
	matrix list e(b)
	foreach i in b se{
		mat `i'`j' = [0\_`i'[post_d6]\_`i'[post_d7]\_`i'[post_d8]\_`i'[post_d9]\_`i'[post_d10]]
	}
	mat ub`j' = b`j'+((1.96)*se`j')
	mat lb`j' = b`j'-((1.96)*se`j')
	mat data`j' = [b`j', ub`j', lb`j']
	svmat data`j'
	rename data`j'1 b`j'
	rename data`j'2 ub`j'
	rename data`j'3 lb`j'
}

recode ub0 ub1 lb0 lb1 (0=.)

#delimit ;
twoway 	(scatter b0 d0 if !missing(b0), color(blue) m(Th) lwidth(thick) msize(medlarge))
		(rcap ub0 lb0 d0 if !missing(b0), color(blue) m(o) msiz(vlarge) lwidth(medthick))
		(scatter b1 d1 if !missing(b1), color(red) m(o) lwidth(thick) msize(medlarge))
		(rcap ub1 lb1 d1 if !missing(b1), color(red) m(o) msiz(vlarge) lwidth(medthick))
		, ytitle(DD Coefficient and 95% CI) xtitle(SABER 11 Decile) 
		xlab(5(1)10, valuelabel) ysc(r(-.1 .3)) ylab(-.1(.1).3) xsize(8) 
		legend(order(3 "Low SES" 1 "High SES")
		position(10) ring(0) size(medlarge))
		yline(0, lc(gray) lpattern(dash) lwidth(thin))
;
#delimit cr
graph export "${outcomes}/Fig10A.pdf", replace

forvalues j=0/1{
	reghdfe score_saberpro_5 post_d6 post_d7 post_d8 post_d9 post_d10  if eligible_sisben==`j', abs(i.icfes_per i.decile) vce(robust)
	cap drop b`j' ub`j' lb`j'
	matrix list e(b)
	foreach i in b se{
		mat `i'`j' = [0\_`i'[post_d6]\_`i'[post_d7]\_`i'[post_d8]\_`i'[post_d9]\_`i'[post_d10]]
	}
	mat ub`j' = b`j'+((1.96)*se`j')
	mat lb`j' = b`j'-((1.96)*se`j')
	mat data`j' = [b`j', ub`j', lb`j']
	svmat data`j'
	rename data`j'1 b`j'
	rename data`j'2 ub`j'
	rename data`j'3 lb`j'
}

recode ub0 ub1 lb0 lb1 (0=.)

#delimit ;
twoway 	(scatter b0 d0 if !missing(b0), color(blue) m(Th) lwidth(thick) msize(medlarge))
		(rcap ub0 lb0 d0 if !missing(b0), color(blue) m(o) msiz(vlarge) lwidth(medthick))
		(scatter b1 d1 if !missing(b1), color(red) m(o) lwidth(thick) msize(medlarge))
		(rcap ub1 lb1 d1 if !missing(b1), color(red) m(o) msiz(vlarge) lwidth(medthick))
		, ytitle(DD Coefficient and 95% CI) xtitle(SABER 11 Decile) 
		xlab(5(1)10, valuelabel) ylab(-0.05(0.05)0.15) xsize(8) 
		legend(order(3 "Low SES" 1 "High SES")
		position(10) ring(0) size(medlarge))
		yline(0, lc(gray) lpattern(dash) lwidth(thin))
;
#delimit cr
graph export "${outcomes}/Fig10B.pdf", replace

forvalues j=0/1{
	reghdfe mw_9 post_d6 post_d7 post_d8 post_d9 post_d10  if eligible_sisben==`j', abs(i.icfes_per i.decile) vce(robust)
	cap drop b`j' ub`j' lb`j'
	matrix list e(b)
	foreach i in b se{
		mat `i'`j' = [0\_`i'[post_d6]\_`i'[post_d7]\_`i'[post_d8]\_`i'[post_d9]\_`i'[post_d10]]
	}
	mat ub`j' = b`j'+((1.96)*se`j')
	mat lb`j' = b`j'-((1.96)*se`j')
	mat data`j' = [b`j', ub`j', lb`j']
	svmat data`j'
	rename data`j'1 b`j'
	rename data`j'2 ub`j'
	rename data`j'3 lb`j'
}

recode ub0 ub1 lb0 lb1 (0=.)

#delimit ;
twoway 	(scatter b0 d0 if !missing(b0), color(blue) m(Th) lwidth(thick) msize(medlarge))
		(rcap ub0 lb0 d0 if !missing(b0), color(blue) m(o) msiz(vlarge) lwidth(medthick))
		(scatter b1 d1 if !missing(b1), color(red) m(o) lwidth(thick) msize(medlarge))
		(rcap ub1 lb1 d1 if !missing(b1), color(red) m(o) msiz(vlarge) lwidth(medthick))
		, ytitle(DD Coefficient and 95% CI) xtitle(SABER 11 Decile) 
		xlab(5(1)10, valuelabel) ylab(-0.05(0.05)0.25) xsize(8) 
		legend(order(3 "Low SES" 1 "High SES")
		position(10) ring(0) size(medlarge))
		yline(0, lc(gray) lpattern(dash) lwidth(thin))
;
#delimit cr
graph export "${outcomes}/Fig11A.pdf", replace

forvalues j=0/1{
	reghdfe ln_wage_9 post_d6 post_d7 post_d8 post_d9 post_d10  if eligible_sisben==`j', abs(i.icfes_per i.decile) vce(robust)
	cap drop b`j' ub`j' lb`j'
	matrix list e(b)
	foreach i in b se{
		mat `i'`j' = [0\_`i'[post_d6]\_`i'[post_d7]\_`i'[post_d8]\_`i'[post_d9]\_`i'[post_d10]]
	}
	mat ub`j' = b`j'+((1.96)*se`j')
	mat lb`j' = b`j'-((1.96)*se`j')
	mat data`j' = [b`j', ub`j', lb`j']
	svmat data`j'
	rename data`j'1 b`j'
	rename data`j'2 ub`j'
	rename data`j'3 lb`j'
}

recode ub0 ub1 lb0 lb1 (0=.)

#delimit ;
twoway 	(scatter b0 d0 if !missing(b0), color(blue) m(Th) lwidth(thick) msize(medlarge))
		(rcap ub0 lb0 d0 if !missing(b0), color(blue) m(o) msiz(vlarge) lwidth(medthick))
		(scatter b1 d1 if !missing(b1), color(red) m(o) lwidth(thick) msize(medlarge))
		(rcap ub1 lb1 d1 if !missing(b1), color(red) m(o) msiz(vlarge) lwidth(medthick))
		, ytitle(DD Coefficient and 95% CI) xtitle(SABER 11 Decile) 
		xlab(5(1)10, valuelabel) ylab(-0.05(0.05)0.25) xsize(8) 
		legend(order(3 "Low SES" 1 "High SES")
		position(10) ring(0) size(medlarge))
		yline(0, lc(gray) lpattern(dash) lwidth(thin))
;
#delimit cr
graph export "${outcomes}/Fig11B.pdf", replace
